class Zcsh {
    public int rob(int[] nums) {
        if (nums.length == 1) return nums[0];

        int pre = 0;
        int current = 0;
        for (int i = 0; i < nums.length - 1; i++) {
            int temp = current;
            current = Math.max(current, pre + nums[i]);
            pre = temp;
        }
        int result = current;
        current = 0;
        pre = 0;

        for (int i = nums.length - 1; i > 0; i--) {
            int temp = current;
            current = Math.max(current, pre + nums[i]);
            pre = temp;
        }
        return Math.max(result, current);
    }
}